package edu.its.dreamteam.web.components;

import org.apache.tapestry.MarkupWriter;
import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.corelib.components.Form;
import org.apache.tapestry.ioc.annotations.Inject;
import org.slf4j.Logger;

import edu.its.dreamteam.daf.beans.Manager;
import edu.its.dreamteam.daf.dao.IManagerDao;
import edu.its.dreamteam.web.base.SimpleBasePage;
import edu.its.dreamteam.web.pages.Start;
import edu.its.dreamteam.web.state.Visit;

public class Login extends SimpleBasePage{
	@Persist("flash")
	private String username;	
	private String password;
	
	
	@Component
	private Form loginForm;
	
	@Inject
	private IManagerDao managerDao;
	
	@Inject
	private Logger _logger;
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
		
	
	public IManagerDao getManagerDao() {
		return managerDao;
	}
	Object onSuccessFromLoginForm() {		
		Object nextPage = null;
		
		try {		

			Manager user = (Manager)getManagerDao().findUserId(getUsername(), getPassword());	
			if(user!=null){
				Visit visit = getVisit();
				visit.noteLogIn(user);
				
				nextPage = Start.class;
			}
			
			
		}
		catch (Exception e) {			
			loginForm.recordError("----Wrong Account");
		}
		
		return nextPage;
		
		
	}

	boolean beforeRender(MarkupWriter writer){
		 return true;
	}
}
